/**
 * zmn员工模块-员工组织数据权限树状列表
 * 文件命名格式：zmn.模块名称.表名称.功能.js
 * @author heguanghua
 * @since 2020/06/01 19:18
 */
layui.define(['form', 'table', 'zmnApp', 'treeTable2', 'treeGrid', 'table'], function (exports) {
    let $ = layui.jquery;
    let form = layui.form;
    let layer = layui.layer;
    let treeTable = layui.treeTable2;
    let treeGrid = layui.treeGrid;
    let zmnApp = layui.zmnApp;
    let table = layui.table;

    var insTb = treeTable.render({
        id: "zmn-table",
        elem: '#zmn-table',
        toolbar: '#zmn-table-tool',
        defaultToolbar: ['filter', 'print'],
        //url: '/role/permit/ajaxTree.action?roleId=' + json.roleId,
        cellMinWidth: 50,
        height: 'full-272',
        // size: 'sm',
        tree: {
            iconIndex: 1,           // 折叠图标显示在第几列
            isPidData: true,        // 是否是id、pid形式数据
            idName: 'dataId',       // id字段名称
            pidName: 'parentId'     // pid字段名称
        },
        cols: [[
            {
                field: '', title: '', width: 50, align: 'center', sort: false, templet: function (d) {
                    return d.level > 0 ? '<input type="checkbox" name="dpermit" lay-filter="ew_tb_checkbox_zmn-table" value="' + d.dataId + '" ' + (d.checked ? 'checked' : '') + ' lay-skin="primary"/>' : '';
                }
            },
            {field: 'dataName', title: '二级合作列表', width: 230, sort: false},
            {
                field: '', title: '账户列表', minWidth: 180, align: 'left', singleLine: false, templet: function (d) {
                    let html = '';
                    d.subors.forEach(function (item, i) {
                        html += '<input type="checkbox" name="account" lay-filter="ew_tb_checkbox_zmn-table2" ' + ' data-parent="' + item.parentId + '" data-type="' + d.dataId + '" value="' + item.dataId + '" ' + (item.checked ? 'checked' : '') + ' title="' + (item.level == 2 ? 1 : 2) + '-' + item.dataName + '" lay-skin="primary"/>'
                    });
                    return html;
                }
            },
        ]],
        reqData: function (data, callback) {     // 数据加载后回调
            $.get(zmnApp.SYS_BASE_URL + '/staff/dpermit/account/ajaxTree.action?staffId=' + json.staffId, function (res) {
                if (res.status == 200) {
                    res.data.forEach(function (item) {
                        if (item.level <= 4) {
                            item.open = true;
                        }
                    });
                    callback(res.data)
                } else {
                    callback(res.message)
                }
                ;
            });
        }
        // end: function(e){},
    });

    form.on('checkbox(ew_tb_checkbox_zmn-table2)', function (obj) {
        let moduleId = obj.value;
        let parent = obj.elem.dataset.parent;
        let $permits = $('input[value=' + parent + ']');
        if ($permits && obj.elem.checked) {
            $permits.prop("checked", true);
            let parent2 = $permits[0].dataset.parent;
            let parent22 = $('input[value=' + parent2 + ']');
            if (parent22) {
                parent22.prop("checked", true);
            }
        }
        layui.form.render('checkbox');
    });
// 快速选择
    treeTable.on('checkbox(zmn-table)', function (obj) {
        let moduleId = obj.data.dataId;
        let checked = obj.checked;

        // 所有会受影响的节点数据
        let childrenNodes = new Array();
        let $permits = $('input[data-type=' + moduleId + ']');
        if ($permits && $permits.length > 0) {
            childrenNodes.push($permits);
        }

        // 递归查询下级子节点
        let childrenPermits = obj.data.children;
        if (childrenPermits && childrenPermits.length > 0) {
            finalQuery(childrenNodes, childrenPermits)
        }

        if (childrenNodes.length == 0) {
            return;
        }

        childrenNodes.forEach(function (item, index) {
            let $this = $(item);
            $this.prop({checked: checked});
        });
        layui.form.render('checkbox');
    });


    // 授权选中/清除全部
    treeTable.on("toolbar(zmn-table)", function (obj) {
        if (obj.event == "assign") {
            layui.layer.confirm('确认提交授权吗？', function () {
                let $ = layui.$;
                var $dpermitIds = $('input[name="dpermit"]');
                var $account = $('input[name="account"]');
                var dpermitIds = new Array();
                var account = new Array();
                if ($dpermitIds.length > 0) {
                    $dpermitIds.each(function (i) {
                        let $this = $(this);
                        if ($this.prop('checked')) {
                            dpermitIds.push($(this).val());
                        }
                    });
                }
                if ($account.length > 0) {
                    $account.each(function (i) {
                        let $this = $(this);
                        if ($this.prop('checked')) {
                            account.push($(this).val());
                        }
                    });
                }
                if (dpermitIds == null || dpermitIds.length == 0) {
                    layui.layer.msg("请选择要分配的数据权限");
                    return;
                }
                zmnPage.ajaxSimple(zmnApp.SYS_BASE_URL + "/staff/dpermit/modify/promotion/assign.action", {
                    keyId: json.staffId,
                    dpermitIds: dpermitIds.join(","),
                    account: account.join(",")
                }, 0);
            });
        } else if (obj.event == "clear") {
            layui.layer.confirm('确认清除全部推广数据权限吗？', function () {
                let $ = layui.$;
                zmnPage.ajaxSimple(zmnApp.SYS_BASE_URL + "/staff/dpermit/modify/promotion/clear.action", {
                    staffId: json.staffId
                }, 1500);
            });
        } else if (obj.event == "addCooperation") {
            layer.open({
                type: 2,
                area: ['1000px', '600px'],
                fixed: false,
                maxmin: true,
                title: '添加合作',
                content: zmnApp.SYS_BASE_URL + '/staff/dpermit/account/list.action?multi=2&callbackUrl=/staff/dpermit/modify/promotion/assign.action&keyId=' + json.staffId
            });
        }
    });


    // 子节点递归
    function finalQuery(childNodeArr, childrenPermits) {
        for (let i = 0; i < childrenPermits.length; i++) {
            let currentNode = childrenPermits[i];

            function fun(currentNode) {
                let permits = currentNode.permits;
                if (permits && permits.length > 0) {
                    for (let j = 0; j < permits.length; j++) {
                        let current = permits[j];
                        childNodeArr.push($('input[data-type=' + current['parentId'] + ']'));
                    }
                }
                let children = currentNode.children;
                if (children && children.length > 0) {
                    for (let k = 0; k < children.length; k++) {
                        let current = children[k];
                        fun(current);
                    }
                }

            }

            fun(currentNode);
        }
    }

    //对外暴露的接口
    exports('staff_dpermit_account_tree', {});
});